import React from "react";
import { Input } from "antd";
interface IVTextState {
  value: string;
}
export default class VText extends React.Component<any, IVTextState> {
  constructor(props: any) {
    super(props);
    this.state = {
      value: props.value || props.default || ""
    }
  }
  shouldComponentUpdate(nextProps) {
    if (nextProps.value !== this.props.value) {
      this.setState({ value: nextProps.value })
    }
    return true;
  }
  handleChange = (ev: React.KeyboardEvent<HTMLInputElement>) => {
    if (ev.code === "Enter") {
      const { value } = this.state;
      const { onChange } = this.props;
      onChange && onChange(value)
    }
  }
  render() {
    const { value } = this.state;
    return <Input {...this.props} defaultValue={this.props.default} value={value}
      onChange={(ev) => this.setState({ value: ev.target.value })} onKeyUp={this.handleChange} />
  }
}